﻿/// <summary>
/// data   :2016-04-27
/// author :fanzhengyong
/// 非组件形式lua script容器，持有者。
/// 通过CallLuaFunction调用lua的函数需要注意的是lua script必须写成module
/// </summary>
using UnityEngine;
using System.Collections;

namespace UEngine
{
    public class LuaHolder
    {
        public string          luaScriptPath;
        public string          luaScriptModuleName;

        private LuaScriptMgr   m_luaScriptMgr  = null;
        private bool           m_isInit        = false;

        public void Init()
        {
            if (
                (!StringHelper.IsValidString(luaScriptPath)) ||
                (!StringHelper.IsValidString(luaScriptModuleName))
            )
            {
                return;
            }

            m_luaScriptMgr = ULuaManager.Instance().GetLuaScriptMgr();
            if (m_luaScriptMgr == null)
            {
                return;
            }

            m_luaScriptMgr.DoFile(luaScriptPath);

            m_isInit = true;
        }

        public void UnInit()
        {
            Util.ClearMemory();
            m_isInit = false;
        }

        /// <summary>
        /// 调用Lua中对应的函数。
        /// </summary>
        /// <param name="name">函数名称</param>
        /// <param name="args"></param>
        public object[] CallLuaFunction(string name, params object[] args)
        {
            if (!m_isInit)
            {
                return null;
            }

            Debug.Log("CallLuaFunction : " + luaScriptModuleName + "." + name);
            return m_luaScriptMgr.CallLuaFunction(luaScriptModuleName + "." + name, args);
        }
    }
}

